//
//  TopViewController.m
//  第六组电影项目
//
//  Created by 6-1 on 14-7-3.
//  Copyright (c) 2014年 www.skedu.com.cn北京尚德智远科技有限公司. All rights reserved.
//

#import "TopViewController.h"
#import "WXNetworkService.h"
#import "TopModel.h"
#import "TopCell.h"
#import "MainViewController.h"
#define kRowHeight 150
@interface TopViewController ()

@end

@implementation TopViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
        self.title=@"Top";
    }
    return self;
}
-(void)loadView
{
    UIView *view=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
    self.view=view;
    topTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight-20-44-49) style:UITableViewStylePlain];
    topTableView.dataSource=self;
    topTableView.delegate=self;
    topTableView.hidden=YES;
    topTableView.rowHeight=kRowHeight;
    topTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    topTableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    [self.view addSubview:topTableView];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self requestTopData];
	// Do any additional setup after loading the view.
}
-(void)requestTopData
{
    NSArray *data=[WXNetworkService topMovieData];
    NSMutableArray *array=[[NSMutableArray alloc]initWithCapacity:data.count];
    for (id dic in data)
    {
        TopModel *top=[[TopModel alloc]initWithContent:dic];
        [array addObject:top];
    }

    NSMutableArray *temp=nil;
    topArray=[[NSMutableArray alloc]init];

    for (int i=0;i<array.count;i++)
    {
        TopModel *topModel=array[i];
        if (i%3==0)
        {
            temp=[[NSMutableArray alloc]initWithCapacity:3];
            [topArray addObject:temp];
        }
        [temp addObject:topModel];
    }
    [self refreshUI];
}

-(void)refreshUI
{
    topTableView.hidden=NO;
    [topTableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [topArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier=@"cell";
    TopCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil)
    {
        cell=[[TopCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.imagesArray=topArray[indexPath.row];
   
    return cell;
}
@end
